

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Utility.Helper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SqlSugar;


namespace CoreCms.Net.Services
{
    /// <summary>
    /// 拼团商品表 接口实现
    /// </summary>
    public class CoreCmsPinTuanGoodsServices : BaseServices<CoreCmsPinTuanGoods>, ICoreCmsPinTuanGoodsServices
    {
        private readonly ICoreCmsPinTuanGoodsRepository _dal;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IServiceProvider _serviceProvider;

        public CoreCmsPinTuanGoodsServices(IUnitOfWork unitOfWork, ICoreCmsPinTuanGoodsRepository dal,
            IServiceProvider serviceProvider
            )
        {
            this._dal = dal;
            base.BaseDal = dal;
            _unitOfWork = unitOfWork;
            _serviceProvider = serviceProvider;
        }

        /// <summary>
        /// 取拼团的商品信息，增加拼团的一些属性，会显示优惠价
        /// </summary>
        /// <param name="pinTuanRuleInfo">拼团规则</param>
        /// <param name="goodsId">商品id</param>
        /// <param name="userId">用户序列</param>
        /// <param name="needRecord">获取拼团记录</param>
        /// <param name="needCheckStock">校验库存</param>
        /// <param name="needGoodSku">获取商品sku</param>
        /// <returns></returns>
        public async Task<CoreCmsGoods> GetGoodsInfo(CoreCmsPinTuanRule pinTuanRuleInfo, int goodsId, int userId, bool needRecord, bool needCheckStock, bool needGoodSku)
        {
            if (pinTuanRuleInfo == null) return null;
            if (goodsId == 0) return null;

            using var container = _serviceProvider.CreateScope();

            var goodsServices = container.ServiceProvider.GetService<ICoreCmsGoodsServices>();
            var pinTuanRecordServices = container.ServiceProvider.GetService<ICoreCmsPinTuanRecordServices>();
            var orderServices = container.ServiceProvider.GetService<ICoreCmsOrderServices>();

            var goodsInfo = await goodsServices.GetGoodsDetail(goodsId, userId, false, "goods", 0, needGoodSku);
            if (goodsInfo == null) return null;

            goodsInfo.pinTuanRule = pinTuanRuleInfo;
            goodsInfo.pinTuanPrice = goodsInfo.price - pinTuanRuleInfo.discountAmount;
            if (goodsInfo.pinTuanPrice < 0) goodsInfo.pinTuanPrice = 0;
            //取拼团记录
            goodsInfo.pinTuanRecordNums = await pinTuanRecordServices.GetCountAsync(p => p.ruleId == pinTuanRuleInfo.id && p.goodsId == goodsInfo.id, true);

            //判断拼团状态
            var dt = DateTime.Now;
            if (goodsInfo.pinTuanRule.startTime > dt)
            {
                goodsInfo.pinTuanRule.pinTuanStartStatus = (int)GlobalEnumVars.PinTuanRuleStatus.notBegun;

                var ts = goodsInfo.pinTuanRule.startTime.Subtract(dt);
                goodsInfo.pinTuanRule.lastTime = (int)ts.TotalSeconds;
            }
            else if (goodsInfo.pinTuanRule.startTime <= dt && goodsInfo.pinTuanRule.endTime > dt)
            {
                goodsInfo.pinTuanRule.pinTuanStartStatus = (int)GlobalEnumVars.PinTuanRuleStatus.begin;

                var ts = goodsInfo.pinTuanRule.endTime.Subtract(dt);
                goodsInfo.pinTuanRule.lastTime = (int)ts.TotalSeconds;
            }
            else
            {
                goodsInfo.pinTuanRule.pinTuanStartStatus = (int)GlobalEnumVars.PinTuanRuleStatus.haveExpired;
            }
            //拼团记录
            if (needRecord)
            {
                var re = await pinTuanRecordServices.GetRecord(pinTuanRuleInfo.id, goodsInfo.id, (int)GlobalEnumVars.PinTuanRecordStatus.InProgress);
                if (re.status)
                {
                    goodsInfo.pinTuanRecord = re.data as List<CoreCmsPinTuanRecord>;
                }
            }
            //判断是否需要检测库存
            if (needCheckStock)
            {
                var checkOrder = orderServices.FindLimitOrder(goodsInfo.product.id, userId, pinTuanRuleInfo.startTime, pinTuanRuleInfo.endTime, 0);
                if (pinTuanRuleInfo.maxGoodsNums > 0)
                {
                    goodsInfo.stock = pinTuanRuleInfo.maxGoodsNums;
                    //活动销售件数
                    goodsInfo.product.stock = pinTuanRuleInfo.maxGoodsNums - checkOrder.TotalOrders;
                    //goodsInfo.buyCount = info.maxGoodsNums - checkOrder.TotalOrders;
                    goodsInfo.buyPinTuanCount = checkOrder.TotalOrders;
                }
                else
                {
                    goodsInfo.buyPinTuanCount = checkOrder.TotalOrders;
                }
            }

            return goodsInfo;
        }



    }
}
